package com.wankuma.hcm.midlet.metwit;

import METwit;

import java.util.Vector;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;

public class StatusDetailScreen extends TextBox implements CommandListener {

	private static StatusDetailScreen instance;

	private Command cmdNewerStatus = new Command("新いｽﾃｰﾀｽ", Command.SCREEN, 0);
	private Command cmdOlderStatus = new Command("古いｽﾃｰﾀｽ", Command.SCREEN, 1);
	private Command cmdReplay = new Command("返信", Command.ITEM, 2);
	private Command cmdDirect = new Command("ﾀﾞｲﾚｸﾄﾒｯｾｰｼﾞ送信", Command.ITEM, 3);
	private Command cmdUserDetail = new Command("ﾕｰｻﾞｰ詳細", Command.ITEM, 4);
	private Command cmdOpenUrl = new Command("URLを開く", Command.ITEM, 5);
	private Command cmdClose = new Command("閉じる", Command.BACK, 300);

	private int timeLineId = Status.FRIENDS;
	private int index;

	private Display display;
	private MainScreen mainScreen;
	private StatusDetailCallback callback;

	public int getTimeLineId() {
		return timeLineId;
	}

	public void setTimeLineId(int _timeLineID) {
		timeLineId = _timeLineID;
	}

	public void addIndex(int cnt) {
		int idx = index + cnt;
		int size = Status.getTimeLine(getTimeLineId()).size();
		if (idx >= size)
			if (index == 0)
				idx = size - 1;
			else
				idx = size;
		index = idx;
		showDetail(true);
	}

	private StatusDetailScreen() {
		super("", "", 1024, TextField.UNEDITABLE);

		this.display = METwit.getDisplay();
		this.mainScreen = METwit.getMainScreen();

		this.addCommand(cmdNewerStatus);
		this.addCommand(cmdOlderStatus);
		this.addCommand(cmdReplay);
		this.addCommand(cmdDirect);
		this.addCommand(cmdUserDetail);
		this.addCommand(cmdOpenUrl);
		this.addCommand(cmdClose);
		this.setCommandListener(this);
	}

	public synchronized static StatusDetailScreen getInstance() {
		if (instance == null) {
			instance = new StatusDetailScreen();
		}
		return instance;
	}

	public synchronized static StatusDetailScreen getInstance(int idx,
			StatusDetailCallback cb) {
		if (instance == null) {
			instance = new StatusDetailScreen();
		}

		instance.callback = cb;
		instance.index = idx;
		instance.showDetail(false);

		return instance;
	}

	private void showDetail(boolean to) {
		Vector tl = Status.getTimeLine(getTimeLineId());
		int size = tl.size();
		Status s = (Status) tl.elementAt(index);

		String title = s.getScreenName() + "(" + s.getName() + ") ("
				+ Integer.toString(index + 1) + "/" + Integer.toString(size)
				+ ")";
		this.setTitle(title);

		if (!to) {
			s.setReaded();
			String str = s.getText() + "\nBy " + s.getScreenName() + "("
					+ s.getName() + ")" + "\nFrom " + s.getSource() + "\nAt "
					+ s.getCreateAt() + "\n" + "SID " + s.getId() + "\n"
					+ "UID " + s.getUserId();

			this.setString(str);
		}
	}

	public void commandAction(Command c, Displayable d) {
		if (c == cmdOlderStatus) {
			Vector tl = Status.getTimeLine(getTimeLineId());
			if (index < tl.size() - 1) {
				index++;
				showDetail(false);
			}
		} else if (c == cmdNewerStatus) {
			if (index > 0) {
				index--;
				showDetail(false);
			}
		} else if (c == cmdClose) {
			callback.detailClosedAction(index);
			display.setCurrent(mainScreen);
		} else if (c == cmdReplay) {
			Vector tl = Status.getTimeLine(getTimeLineId());
			Status s = (Status) tl.elementAt(index);
			callback.detailClosedAction(index);
			display.setCurrent(UpdateScreen.getInstance("@" + s.getScreenName()
					+ " "));
		} else if (c == cmdDirect) {
			Vector tl = Status.getTimeLine(getTimeLineId());
			Status s = (Status) tl.elementAt(index);
			callback.detailClosedAction(index);
			display.setCurrent(UpdateScreen.getInstance("d "
					+ s.getScreenName() + " "));
		} else if (c == cmdUserDetail) {

		} else if (c == cmdOpenUrl) {
			Vector tl = Status.getTimeLine(getTimeLineId());
			String str = ((Status) tl.elementAt(index)).getText();
			int cidx = 0;
			Vector urls = new Vector();
			while (cidx < str.length()) {
				cidx = str.indexOf("http://", cidx);
				if (cidx >= 0) {
					StringBuffer sb = new StringBuffer("http://");
					cidx += 7;
					while (0x21 <= str.charAt(cidx) && str.charAt(cidx) <= 0x7E) {
						sb.append(str.charAt(cidx++));
						if (cidx >= str.length())
							break;
					}
					urls.addElement(sb.toString());
				} else
					break;
			}
			OpenUrlScreen scr = new OpenUrlScreen(urls, this);
			display.setCurrent(scr);
		}
	}
}
